const Db = require('../services/mysql')
const { Fb, Moment, Op, Dev } = require('../class/utils')
const _ = require('lodash')

class User {
    constructor() {

    }

    /**
     * 创建用户
     * @param {*} where 
     */
    create(form) {
        return Db.Sysuser.create({
            uname: form.uname,
            upass: form.upass,
            tname: form.tname,
            sex: form.sex || '女',
            tel: form.tel,
            email: form.email,
            qq: form.qq,
            jg: form.jg,
            addr: form.addr,
            utype: form.utype,
            delstatus: form.delstatus || '否',
            remark: form.remark,
            status: form.status || '使用中',
            createdAt: Moment.current()
        })
    }

    /**
    * 查找用户列表
    * @param {*} where 
    */
    select(form, nowPage, pageSize) {
        let tname = form.name || '';
        let uname = form.name || '';
        let sex = form.sex || '';
        return Db.Sysuser.findAndCountAll({
            where: {
                [Op.or]: [{
                    tname: {
                        [Op.like]: '%' + tname + '%'
                    },
                }, {
                    uname: {
                        [Op.like]: '%' + uname + '%'
                    }
                }],
                sex: {
                    [Op.like]: '%' + sex + '%'
                },
                delstatus: '否'
            },
            offset: pageSize * (nowPage - 1),
            limit: pageSize,
        })
    }

    /**
     * 用户详情
     * @param {*} where 
     */
    details(id) {
        return Db.Sysuser.findById(id)
    }

    /**
    * 获取用户密码
    * @param {*} where 
    */
    getPassword(uname) {
        return Db.Sysuser.findOne({
            where: { 'uname': uname }
        })
    }

    /**
     * 修改用户信息
     * @param {*} where 
     */
    update(from) {
        return Db.Sysuser.update({
            tname: from.tname,
            upass: from.upass
        }, {
                where: {
                    id: from.id
                },
            })
    }

    /**
     * 注销用户
     * @param {*} where 
     */
    updateStatus(from) {
        return Db.Sysuser.update({
            status: '已注销'
        }, {
                where: {
                    id: from.id
                },
            })
    }

    /**
     * 删除用户
     * @param {*} where 
     */
    delete(from) {
        return Db.Sysuser.update({
            delstatus: '是',
            status: '已删除'
        }, {
                where: {
                    id: from.id
                },
            })
    }
}

module.exports = new User()